package com.itheima.controller;

import com.itheima.pojo.User;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;


import java.util.List;

@RestController
@RequestMapping("/movie")
@DefaultProperties(defaultFallback = "defaultAllMethod")
public class MovieController {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping(value = "/look", method = RequestMethod.GET)
    //@HystrixCommand(fallbackMethod = "defaultMethod")
    @HystrixCommand
    public User look() {
        /*List<ServiceInstance> serviceInstances = discoveryClient.getInstances("user-provider");
        ServiceInstance serviceInstance = serviceInstances.get(0);
        User user = restTemplate.getForObject("http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/user/2", User.class);*/
        User user = restTemplate.getForObject("http://user-provider/user/2", User.class);
        System.out.println("用户名为:" + user.getName() + ",用户id为:" + user.getId());
        System.out.println("用户验证成功,您可以购买电影票了!");
        return user;
    }

    public User defaultMethod() {
        User user = new User();
        user.setId(1111);
        user.setName("该用户为默认的用户数据");
        return user;
    }

    //全局的降级方法不得有参数，必须有返回值
    public User defaultAllMethod() {
        User user = new User();
        user.setId(1111);
        user.setName("该用户为默认的用户数据全局");
        return user;
    }
}
